package monotonic_stack;

import java.util.*;

public class Class739 {

    public int[] dailyTemperatures(int[] temperatures) {
        Deque stack = new LinkedList<>();
        stack.push(0);
        int[]  result=new int[temperatures.length];
        for (int i = 1; i < temperatures.length ; i++) {
            while (!stack.isEmpty()&&temperatures[i]>temperatures[(int) stack.peek()]){
                result[(int) stack.peek()]=i-(int)stack.pop();
            }
            stack.push(i);
        }
        return  result;
    }


    public  int[]  dailyTempratures(int[]  temp) {
        int length = temp.length;
        int[] res = new int[length];
        Deque stack = new LinkedList<>();
        stack.push(0);
        for (int i = 1; i < length; i++) {

            if (temp[i] <= temp[(int) stack.peek()]) {
                stack.push(i);
            }
            else {
                while (!stack.isEmpty() && temp[i] > temp[(int) stack.peek()]) {
                    res[(int) stack.peek()] = i - (int) stack.peek();
                    stack.pop();
                }
                stack.push(i);
            }

        }
        return res;
    }




    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            map.put(nums1[i], i);
        }

        int[] res = new int[nums1.length];
        Stack<Integer> stack = new Stack<>();
        Arrays.fill(res, -1);

        for (int i = 0; i < nums2.length; i++) {
            while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) {
                int pre = nums2[stack.pop()];
                if (map.containsKey(pre)) {
                    res[map.get(pre)] = nums2[i];
                }
            }
            stack.push(i);
        }

        return res;
    }


//TODO:2023/8/2  下一个更大的元素 我的写法 没有用到单调栈  暴力解决
    public int[] find(int[] arr,int[] arr1){
        int[] res=new int[arr.length];
        Arrays.fill(res,-1);
        Map map  = new HashMap<Integer,Integer>();
        for(int i=0;i<arr1.length;i++){
            map.put(arr1[i],i);
        }
        for(int i=0;i<arr.length;i++){
            int index=(int)map.get(arr[i]);
            for(int j=index+1;j<arr1.length;j++){
                if(arr1[j]>arr[i]){
                    res[i]=arr1[j];
                    break;
                }
            }
        }
        return res;
    }


//TODO:2023/8/2  下一个更大的元素er   我的实现
    public int[] find(int[] nums){

        int  length=nums.length;
        int[] res=new int[length];
        Arrays.fill(res,-1);

        int[]  xunhuan=new int[length*2];

        for(int i=0;i<length*2;i++){
            xunhuan[i]=nums[i%length];
        }
        for (int i = 0; i < nums.length; i++) {
            int j=i+1;
            while (j<length*2){
                if (xunhuan[j]>nums[i]){
                    res[i]=xunhuan[j];
                    break;
                }
                j++;
            }
        }

        return  res;

    }









    public static void main(String[] args) {
        Class739 class739 = new Class739();

        int[] arr1={1,2,1};
        int[] arr={1,3,4,2};
        int[] ints = class739.find(arr1);
        for (int a:ints
             ) {
            System.out.println(a);

        }
    }




}
